﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;

namespace FRL.DebtChased.ClientEditor
{

	/// <summary>
	/// Declaration of ClientInstructionBatchCtrl class
	/// Control for editing the list of batch instruction groupings
	/// for the currently loaded client
	/// </summary>
	public partial class ClientInstructionBatchCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Constants

		private const int TopInsertRow = 1;
		private const int FooterRowCount = 2;

		/////////////////////////////////////////////////////////////
		// Construction

		public ClientInstructionBatchCtrl()
		{

			// Load xaml
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void butAdd_Click(object sender, RoutedEventArgs e)
		{

			// Check the name
			string strName = txtAddName.Text.Trim();
			if (strName.Length < 4)
			{
				MessageBoxes.PopupMessage("Instruction batch name must have at least 4 characters.");
				return;
			}

			// Add a new row
			_addRows(1);

			// Set controls
			TextBox txtName = null;
			TextBlock tbInUse = null;
			Button butDelete = null;
			_getControls(gridList.RowDefinitions.Count - FooterRowCount - 1, out txtName, out tbInUse, out butDelete);
			txtName.Text = strName;
			tbInUse.Text = "NO";
			butDelete.IsEnabled = true;

		}

		void butDelete_Click(object sender, RoutedEventArgs e)
		{

			// Get the row index
			Button butDelete = sender as Button;
			int iRowIndex = Grid.GetRow(butDelete);

			// Confirm delete
			if (!MessageBoxes.PopupOption("Delete the selected client instruction batch?"))
				return;

			// Delete the row
			_deleteRows(iRowIndex,1);

		}


		/////////////////////////////////////////////////////////////
		// Operations

		public void Set(ClientTransport clientTransport)
		{

			// If need to create more rows...
			int iRows = gridList.RowDefinitions.Count - 1 - FooterRowCount;
			if (iRows < clientTransport.ClientInstructionBatchList.Count)
				_addRows(clientTransport.ClientInstructionBatchList.Count - iRows);
			else if (iRows > clientTransport.ClientInstructionBatchList.Count)
			{
				int iDeleteCount = iRows - clientTransport.ClientInstructionBatchList.Count;
				_deleteRows(
					gridList.RowDefinitions.Count - FooterRowCount - iDeleteCount,
					iDeleteCount
				);
			}

			// Load the rows
			for(int i1=0; i1<clientTransport.ClientInstructionBatchList.Count; i1++ )
			{

				// Set the row
				TextBox txtName = null;
				TextBlock tbInUse = null;
				Button butDelete = null;
				_getControls(1 + i1, out txtName, out tbInUse, out butDelete);

				// Set controls
				ClientInstructionBatchTransport cib = clientTransport.ClientInstructionBatchList[i1];
				txtName.Tag = cib.ClientInstructionBatchId;
				txtName.Text = cib.Name;
				tbInUse.Text = cib.InUse ? "YES" : "NO";
				butDelete.IsEnabled = (!cib.InUse);
				
			}			

		}

		public void Get( ClientTransport clientTransport, List<string> Errors )
		{

			// Populate the client object with details from the control
			// and find errors in the user input

			// Load the batches
			for( int iRowIndex = 1; iRowIndex < (gridList.RowDefinitions.Count - FooterRowCount); iRowIndex++ )
			{

				// Get the controls
				TextBox txtName = null;
				TextBlock tbInUse = null;
				Button butDelete = null;
				_getControls(iRowIndex, out txtName, out tbInUse, out butDelete);

				// Get id
				int iClientInstructionBatchId = Convert.ToInt32(txtName.Tag);

				// Add to client
				ClientInstructionBatchTransport batch = new ClientInstructionBatchTransport()
				{
					ClientInstructionBatchId = iClientInstructionBatchId,
					Name = txtName.Text.Trim()
				};
				clientTransport.ClientInstructionBatchList.Add(batch);

				// Check name
				if(batch.Name.Length < 4)
					Errors.Add("Client instruction batch names must have 4 characters.");

			}

		}

		private void _addRows(int iCount)
		{

			// Shift existing controls down
			gridList.ShiftRowsDown(gridList.RowDefinitions.Count - FooterRowCount, iCount);

			// Add rows and controls
			for (int i1 = 0; i1 < iCount; i1++)
			{

				// Calc new row index
				int iRowIndex = gridList.RowDefinitions.Count - FooterRowCount;

				// Create row
				RowDefinition newRow = new RowDefinition();
				newRow.Height = new GridLength(0, GridUnitType.Auto);
				gridList.RowDefinitions.Insert(iRowIndex, newRow);

				TextBox txtName = new TextBox();
				txtName.Tag = 0;
				txtName.Style = this.Resources["FatBlueTextBoxStyle"] as Style;
				gridList.Children.Add(txtName);
				Grid.SetColumn(txtName, 0);
				Grid.SetRow(txtName, iRowIndex);

				TextBlock tbInUse = new TextBlock();
				tbInUse.Style = this.Resources["FatBlueTextBlockStyle"] as Style;
				tbInUse.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;					 
				gridList.Children.Add(tbInUse);
				Grid.SetColumn(tbInUse, 1);
				Grid.SetRow(tbInUse, iRowIndex);

				Button butDelete = new Button();
				butDelete.Content = "Delete...";
				butDelete.Click += new RoutedEventHandler(butDelete_Click);
				butDelete.Style = this.Resources["FatBlueButtonStyle"] as Style;
				gridList.Children.Add(butDelete);
				Grid.SetColumn(butDelete, 2);
				Grid.SetRow(butDelete, iRowIndex);

			}

		}

		private void _deleteRows( int iRowIndex, int iCount )
		{

			// Delete the rows
			gridList.DeleteRows(iRowIndex, iCount);

		}

		private void _getControls(int iRowIndex, out TextBox txtName, out TextBlock tbInUse, out Button butDelete)
		{

			// Reset function parameters
			txtName = null;
			tbInUse = null;
			butDelete = null;

			// Loop through controls
			foreach (FrameworkElement el in gridList.Children)
			{

				// Ignore non-framework elements
				if(el==null)
					continue;

				// If row matches...
				if (Grid.GetRow(el) == iRowIndex)
				{

					// Compare types
					if (el is TextBox)
					{
#if DEBUG
						Debug.Assert(txtName == null);
						Debug.Assert(Grid.GetColumn(el) == 0);
#endif // #if DEBUG
						txtName = el as TextBox;
					}
					else if (el is TextBlock)
					{
#if DEBUG
						Debug.Assert(tbInUse == null);
						Debug.Assert(Grid.GetColumn(el) == 1);
#endif // #if DEBUG
						tbInUse = el as TextBlock;
					}
					else if (el is Button)
					{
#if DEBUG
						Debug.Assert(butDelete == null);
						Debug.Assert(Grid.GetColumn(el) == 2);
#endif // #if DEBUG
						butDelete = el as Button;
					}
#if DEBUG
					else
						Debug.Assert(false);
#endif // #if DEBUG

				}

			}

			// Check result
#if DEBUG
			Debug.Assert(txtName != null);
			Debug.Assert(tbInUse != null);
			Debug.Assert(butDelete != null);
#endif // #if DEBUG

		}

	}

}
